package sparrowGui.components.item
{
	import flash.display.DisplayObjectContainer;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * 按钮组件，相比Item监听鼠标操作相关事件
	 * 
	 * 例子如下
	 * 
	 * var itm:SItem = new SListItem();
		itm.update("按钮文字");
		addChild(itm);
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SButton extends SItem
	{
		/**
		 * 构造按钮组件
		 * @param uiVars
		 */
		public function SButton(uiVars:Object=null)
		{
			super(uiVars);
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
			super.setUI(uiVars);
			
			if(skin is Sprite)
			{
				(skin as Sprite).buttonMode = true;
				if(hitTestState is Sprite)
					(skin as Sprite).hitArea = (hitTestState as Sprite);
			}
			
			// 如果皮肤是剪辑或者是容器就添加相应事件进行控制
			if(!(skin is SimpleButton) && (skin is DisplayObjectContainer))
			{
//				if(hitTestState){
//					skin.hitArea(hitTestState);
////					hitTestState.visible = false;
//				}
				addSkinListen();
			}
		}
		
		override protected function addSkinListen():void
		{
			// ROLL_OVER无视子对象事件,MOUSE_OVER相反
			skin.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
			skin.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			skin.addEventListener(MouseEvent.MOUSE_UP,onEvtOut);
			skin.addEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}
		
		override protected function removeSkinListen():void
		{
			skin.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			skin.removeEventListener(MouseEvent.MOUSE_UP,onEvtOut);
			skin.removeEventListener(MouseEvent.ROLL_OVER,onRollOver);
			skin.removeEventListener(MouseEvent.ROLL_OUT,onRollOut);
			if(!skin.stage) return;
			skin.stage.removeEventListener(MouseEvent.MOUSE_UP, onEvtOut);
			skin.stage.removeEventListener(Event.MOUSE_LEAVE, onEvtOut);
		}
		
		/**
		 * 鼠标经过事件
		 * @param e
		 */
		protected function onRollOver(e:MouseEvent):void
		{
			if(!enabled)
				return;
			
			changeState(OVERSTATE_NAME);
		}
		
		/**
		 * 鼠标按下
		 * @param e
		 */
		protected function onMouseDown(e:MouseEvent):void
		{
			if(!enabled)
				return;
			
			changeState(DOWNSTATE_NAME);
			if(!skin || !skin.stage) return;
			
			skin.stage.addEventListener(MouseEvent.MOUSE_UP, onEvtOut);
			skin.stage.addEventListener(Event.MOUSE_LEAVE,onEvtOut);
			
			skin.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			skin.removeEventListener(MouseEvent.ROLL_OVER,onRollOver);
			skin.removeEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}
		
		/**
		 * 移开事件
		 * @param e
		 */
		protected function onEvtOut(e:Event):void
		{
			if(!enabled)
				return;
			
			var me:MouseEvent = e as MouseEvent;
			var isOver:Boolean = (me && me.currentTarget==skin);
//				if(me.stageX>=pt.x && me.stageX<=(pt.x + skin.width)
//				&& me.stageY>=pt.y && me.stageY<=(pt.y + skin.height))
//					isOver = true;
			if(!isOver)
				changeState(UPSTATE_NAME);
			else
				changeState(OVERSTATE_NAME);
			
			if(!skin || !skin.stage) return;
			skin.stage.removeEventListener(MouseEvent.MOUSE_UP, onEvtOut);
			skin.stage.removeEventListener(Event.MOUSE_LEAVE, onEvtOut);
			
			skin.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
			skin.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			skin.addEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}
		
		/**
		 * 鼠标移开事件
		 */
		protected function onRollOut(e:MouseEvent):void
		{
			if(!enabled)
				return;
			
			changeState(UPSTATE_NAME);
		}
		
		///////////////////////////////////
		// get/set
		//////////////////////////////////
		
		
		override public function getDefaultUIName():String
		{
			return "button";
		}
	}
}